<!--
 * @Author: MarioGo
 * @Date: 2021-11-06 14:33:50
 * @LastEditTime: 2021-11-12 12:20:21
 * @LastEditors: MarioGo
 * @Description: 文件描述
 * @FilePath: /zheye/src/components/GlobalHeader.vue
 * 可以输入预定的版权声明、个性签名、空行等
-->
<template>
  <nav class="navbar navbar-dark bg-primary justify-content-between mb-4 px-4">
    <router-link to="/" class="navbar-brand">哲也专栏</router-link>
    <ul v-if="!user.isLogin" class="list-inline-item mb-0">
      <li class="list-inline-item">
        <router-link to="/login" class="btn btn-outline-light my-2"> 登录 </router-link>
      </li>
      <li class="list-inline-item">
        <router-link to="/signup" class="btn btn-outline-light my-2"> 注册 </router-link>
      </li>
    </ul>
    <ul v-else class="list-inline-item mb-0">
      <li class="list-inline-item">
        <dropdown :title="`您好 ${user.name}`">
          <dropdown-item
            ><router-link to="/create" class="dropdown-item">新建文章</router-link></dropdown-item
          >
          <dropdown-item
            ><router-link :to="`/column/${user.column}`" class="dropdown-item"
              >我的专栏</router-link
            ></dropdown-item
          >
          <!-- <dropdown-item disabled><a href="#" class="dropdown-item">个人中心</a></dropdown-item> -->
          <dropdown-item closeAfterClick
            ><router-link to="/edit" class="dropdown-item">编辑资料</router-link></dropdown-item
          >
          <dropdown-item
            ><a href="#" class="dropdown-item" @click.prevent="handleLogout"
              >退出登陆</a
            ></dropdown-item
          >
        </dropdown>
      </li>
    </ul>
  </nav>
</template>

<script lang="ts">
import Dropdown from './Dropdown.vue';
import DropdownItem from './DropdownItem.vue';
import { defineComponent, PropType } from 'vue';
import { useRouter } from 'vue-router';
import store, { UserProps } from '../store';
import createMessage from '../components/createMessage';

export default defineComponent({
  name: 'GlobalHeader',
  components: {
    Dropdown,
    DropdownItem,
  },
  props: {
    user: {
      type: Object as PropType<UserProps>,
      required: true,
    },
  },
  setup() {
    const router = useRouter();
    const handleLogout = () => {
      // 多个异步调用组合
      store.dispatch('singoutActionFetch');
      createMessage('退出登录成功，2秒后跳转到首页！', 'success');
      setTimeout(() => {
        router.push('/');
      }, 2000);
    };
    return {
      handleLogout,
    };
  },
});
</script>

<style scoped></style>
